﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Security;
using Core.Library;

namespace DataObject.Customer
{
    public class CustomerController
    {
        /***************************************
        = Back End
        -------------------------------------- */
        public static List<CustomerObject> GetListSearchCustomer(int typeId, string keyword, int pageIndex, int pageSize)
        {
            string filter = string.Empty;
            switch (typeId)
            {
                case 1: // Số điện thoại tại cửa hàng                    
                    filter = string.Format("Mobile LIKE N'%{0}%'", keyword);
                    break;
                case 2: // Contact Name
                    filter = string.Format("ContactName LIKE N'%{0}%'", keyword);
                    break;
                case 3: // Email
                    filter = string.Format("Email LIKE N'%{0}'%", keyword);
                    break;
                case 4: // Username
                    filter = string.Format("Username LIKE N'%{0}%'", keyword);
                    break;
            }

            return ObjectHelper.FillCollection<CustomerObject>(Action.Reader_GetListPaging("Customer", "CustomerID DESC", filter, pageIndex, pageSize));
        }

        public static void Customer_Delete(int customerId)
        {
            CustomerDAL.Customer_Delete(customerId);
        }

        public static CustomerObject Customer_GetById(int customerId)
        {
            return ObjectHelper.FillObject<CustomerObject>(CustomerDAL.Customer_GetById(customerId));
        }

        public static CustomerObject Customer_GetByUsername(string username)
        {
            return ObjectHelper.FillObject<CustomerObject>(CustomerDAL.Customer_GetByUsername(username));
        }

        public static CustomerObject Customer_GetByEmail(string email)
        {
            return ObjectHelper.FillObject<CustomerObject>(CustomerDAL.Customer_GetByEmail(email));
        }

        public static string GetCustomerInfo(int customerId, string columnName)
        {
            try
            {
                var o = Customer_GetById(customerId);
                if (o != null)
                {
                    switch (columnName)
                    {
                        case "Mobile":
                            return o.Mobile;
                        case "ContactName":
                            return o.ContactName;
                        case "Username":
                            return o.Username;
                        case "Email":
                            return o.Email;
                        case "Address":
                            return o.Address;
                    }
                }
            }
            catch { return string.Empty; }
            return string.Empty;
        }

        public static int Customer_CheckUsernameExist(string username)
        {
            return CustomerDAL.Customer_CheckUsernameExist(username);
        }

        public static int Customer_CheckEmailExist(string email)
        {
            return CustomerDAL.Customer_CheckEmailExist(email);
        }

        public static DataTable GetListSearch(int type, string keyword)
        {
            return CustomerDAL.GetListSearch(type, keyword);
        }

        /***************************************
        = Front End
        -------------------------------------- */
        public static int ValidateCustomer(string username, string password)
        {
            return CustomerDAL.ValidateCustomer(username, password);
        }

        public static bool Login(string username, string password)
        {
            Logout();
            if (ValidateCustomer(username, password) > 0)
            {
                FormsAuthentication.Initialize();
                var name = username;
                var ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, DateTime.Now.AddMinutes(45), true, "", FormsAuthentication.FormsCookiePath);
                Trace.Write(FormsAuthentication.FormsCookiePath + FormsAuthentication.FormsCookieName);
                var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
                HttpContext.Current.Response.SetCookie(cookie);                

                return true;
            }
            return false;
        }

        public static void Logout()
        {
            FormsAuthentication.SignOut();
            if (HttpContext.Current.Response.Cookies["name"] != null && HttpContext.Current.Response.Cookies["pass"] != null)
            {
                HttpContext.Current.Response.Cookies["name"].Expires = DateTime.Now.AddDays(-1);
                HttpContext.Current.Response.Cookies["pass"].Expires = DateTime.Now.AddDays(-1);
            }
        }

        public static int Customer_Create(CustomerObject o)
        {
            return CustomerDAL.Customer_Create(o);
        }

        public static void Customer_Update(int customerId, string contactName, string mobile, string email, string address)
        {
            CustomerDAL.Customer_Update(customerId, contactName, mobile, email, address);
        }

        public static void Customer_Update_Status(int customerId, bool isActive)
        {
            CustomerDAL.Customer_Update_Status(customerId, isActive);
        }

        public static void Customer_ChangePassword(int customerId, string newPassword)
        {
            CustomerDAL.Customer_ChangePassword(customerId, newPassword);
        }
    }
}
